#include "Table.h"
#include "Irq.h"
#include "Pic.h"
#include "Pit.h"
#include "Debug.h"
#include "Task.h"
#include "Sem.h"
#include "Vm.h"
#include "utility/Types.h"
#include "utility/Instruction.h"
#include "utility/Memory.h"

// 1kb
uint8_t tempStack1[1024];
uint8_t tempStack2[1024];
TaskType firstTask;
TaskType secondTask;


void MoveToFirstTask(void)
{
    TaskType * curr = GetCurrentTask();
    ASSERT(curr != 0);

    TssDescType * tss = &(curr->tss);
    __asm__ __volatile__(
        // 模拟中断返回，切换入第1个可运行应用进程
        // 不过这里并不直接进入到进程的入口，而是先设置好段寄存器，再跳过去
        "push %[ss]\n\t"			// SS
        "push %[esp]\n\t"			// ESP
        "push %[eflags]\n\t"        // EFLAGS
        "push %[cs]\n\t"			// CS
        "push %[eip]\n\t"		    // ip
        "iret\n\t"::[ss]"r"(tss->ss),  [esp]"r"(tss->esp), [eflags]"r"(tss->eflags),
        [cs]"r"(tss->cs), [eip]"r"(tss->eip));
}


// this function is called in Origin.S
void InitOriginOS(MemoryInfoType *memoryInfo)
{
    InitTable();
    InitDebug();
    InitVM(memoryInfo);
    InitPIC();
    InitPIT();

    InitTaskManager();
}


void Start()
{
    InitFirstTask();
    MoveToFirstTask();
}